package com.wyd.jwpkglxt_ssm.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtil {

    private static String SING = "token!Q?w$3@R&";

    /**
     * @description: <br>
     * 生成token
     * @Param: [map]
     * @Return: java.lang.String
     * @Author: 文永达
     * @Date: 2021/7/10 17:30
     * @param map
     */
    public static String getToken(Map<String,String> map){

        Calendar instance = Calendar.getInstance();
        //默认1天过期
        instance.add(Calendar.DATE,10);
        //创建jwt builder
        JWTCreator.Builder builder = JWT.create();
        //payload
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SING));;//指定令牌过期时间
        return token;
    }
    /**
     * @description: <br>
     * 验证token 合法性
     * @Param: [token]
     * @Return: void
     * @Author: 文永达
     * @Date: 2021/7/10 17:31
     * @return
     */
    public static DecodedJWT verify(String token){
        return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

    /**
     * @description: <br>
     * 获取token 信息
     * @Param: [token]
     * @Return: com.auth0.jwt.interfaces.DecodedJWT
     * @Author: 文永达
     * @Date: 2021/7/10 17:52
     */
    //public static DecodedJWT tokenInfo(String token){
    //    return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    //}
}
